package com.chookss.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.chookss.R;
import com.chookss.tiku.adapter.BottomListsAdapter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;


public class BottomListsDialog extends Dialog {
    private ListView lv;
    private BottomListsAdapter adapter;
    private List<String> lists = new ArrayList<>();
    private Context context;
    private SelectDialogListener listener = null;

    public BottomListsDialog(@NonNull Context context, List<String> data, SelectDialogListener listener) {
        super(context);
        this.context = context;
        this.lists = data;
        this.listener = listener;
        initLayoutRes();
    }

    public BottomListsDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initLayoutRes();
    }

    public void setSelect(int position) {
        adapter.setSelect(position);
    }

    private void initLayoutRes() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_bottom_lists, null);
        lv = view.findViewById(R.id.lvBottom);
        adapter = new BottomListsAdapter(context, lists);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                if (listener != null) {
                    listener.onListItemClick(position, lists);
                }
            }
        });
        setContentView(view);
        initBottom();
    }

    protected void initBottom() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        Window wm = getWindow();
        lp.width = lp.MATCH_PARENT;
        lp.height = lp.WRAP_CONTENT;
        lp.dimAmount = 0.6f;
        wm.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        wm.setGravity(Gravity.BOTTOM);
        wm.setWindowAnimations(R.style.DialogFullWidthDimNotEnabled);
        getWindow().setAttributes(lp);
    }

    protected void safeDismiss() {
        if (isShowing()) {
            dismiss();
        }
    }
}
